package com.hhkj.admin.student.service.base.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.hhkj.admin.student.domain.StudentCourse;
import com.hhkj.admin.student.mapper.StudentCourseMapper;
import com.hhkj.admin.student.model.dopo.StudentCourseDo;
import com.hhkj.admin.student.model.dopo.StudentCoursePo;
import com.hhkj.admin.student.model.dopo.StudentPo;
import com.hhkj.admin.student.model.req.StudentBatchReq;
import com.hhkj.admin.student.model.req.StudentCourseReq;
import com.hhkj.admin.student.model.req.StudentReq;
import com.hhkj.admin.student.service.base.StudentCourseService;
import com.ruoyi.common.base.biz.service.base.BaseServiceImpl;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 学员课程信息关联Service业务层处理
 *
 * @author ruoyi
 * @since 2023-05-17
 */
@Service
public class StudentCourseServiceImpl
        extends BaseServiceImpl<StudentCourse, StudentCourseDo, StudentCoursePo, StudentCourseMapper>
        implements StudentCourseService {

    @Override
    public List<StudentCourse> listByIdList(List<Long> idList) {
        if (ObjectUtil.isEmpty(idList)) {
            return new ArrayList<>();
        }
        LambdaQueryWrapper<StudentCourse> queryWrapper = new LambdaQueryWrapper<StudentCourse>()
                .in(StudentCourse::getId, idList);
        return list(queryWrapper);
    }

    /**
     * 统计条数
     *
     * @param studentCourse 条件
     * @return 条数
     */
    @Override
    public int countByCondition(StudentCourse studentCourse) {
        return this.getBaseMapper().countByDo(studentCourse);
    }

    /**
     * 批量修改过期学员状态
     *
     * @param idList 学员课程主键
     */
    @Override
    public void updateBatchStatusByIdList(List<Long> idList, String statusCode) {
        this.getBaseMapper().updateBatchStatusByIdList(idList, statusCode);
    }


    /**
     * 清除被设置为‘未审核’状态的学员的到期日期和题库类型
     *
     * @param idList id集合
     */
    @Override
    public void cleanInfoByIdList(List<Long> idList) {
        this.getBaseMapper().cleanInfoByIdList(idList);
    }

    /**
     * 批量设置学员单价
     *
     * @param idList           学员idList
     * @param studentCourseReq 学员单价
     */
    @Override
    public void updateStudentCourseByIdList(List<Long> idList, StudentCourseReq studentCourseReq) {
        this.getBaseMapper().updateStudentCourseByIdList(idList, studentCourseReq);
    }

    /**
     * 统计批次下全部学员的缴费总金额
     *
     * @param batchId 批次id
     * @return 缴费总金额
     */
    @Override
    public BigDecimal countBatchStudentPrice(Long batchId) {
        return this.getBaseMapper().countBatchStudentPrice(batchId);
    }

    /**
     * 查询批次下学员列表
     *
     * @param studentBatchReq 批次号id
     * @return 学员
     */
    @Override
    public List<StudentPo> listBatchStudentPoByDo(StudentBatchReq studentBatchReq) {
        return this.getBaseMapper().listBatchStudentPoByDo(studentBatchReq);
    }

    /**
     * 查询学员信息列表（包含关联表信息）
     *
     * @param studentReq 学员信息
     * @return 学员信息集合
     */
    @Override
    public List<StudentPo> selectStudentRelevancyList(StudentReq studentReq) {
        return this.getBaseMapper().selectStudentRelevancyList(studentReq);
    }

    /**
     * 根据条件查询学员科目
     *
     * @param coursePo 参数
     * @return 学员科目集合
     */
    @Override
    public List<StudentCourse> listStudentCoursePoByDo(StudentCoursePo coursePo) {
        return this.getBaseMapper().listStudentCoursePoByDo(coursePo);
    }

    /**
     * 解绑微信
     *
     * @param studentBatchReq 传参
     * @return 影响行数
     */
    @Override
    public int cancelBind(StudentBatchReq studentBatchReq) {
        return this.getBaseMapper().cancelBind(studentBatchReq);
    }

    @Override
    public List<StudentCourse> listByIdCardList(List<String> idCardList, List<String> statusList) {
        if (ObjectUtil.isEmpty(idCardList)) {
            return new ArrayList<>();
        }
        List<String> distinct = CollectionUtil.distinct(idCardList);
        return this.getBaseMapper().listByIdCardList(distinct, statusList);
    }
}
